package com.wzz.seckillone.controller;

import com.wzz.seckillone.pojo.User;
import com.wzz.seckillone.service.GoodsService;
import com.wzz.seckillone.service.UserService;
import com.wzz.seckillone.vo.GoodsVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.util.Date;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private UserService userService;

    @Autowired
    private GoodsService goodsService;

    /**
     * @Method
     * @Author wzz
     * @Version  1.0
     * @Description 加入shiro后 秒杀商品跳转
     * @param
     * @Return
     * @Exception
     * @Date 2022/3/18 15:26
     */
    @GetMapping("/toGoodsList")
    public String toGoodsList(Model model){
        model.addAttribute("goodsList",goodsService.findGoodsVo());
        return "goodsList";
    }
    //弃用
    @GetMapping("/toList")
    public String toList(HttpSession session,Model model,@CookieValue("userTicket") String ticket){
        if (StringUtils.isEmpty(ticket)){
            return "login";
        }
        User user = userService.getUserByTicket("user:"+ticket);
        if (user == null){
            return "login";
        }
        model.addAttribute("goodsList",goodsService.findGoodsVo());
        model.addAttribute("user",user);
        return "goodsList";
    }
    /**
     * @Method
     * @Author wzz
     * @Version  1.0
     * @Description 加入shiro后的秒杀页面
     * @param
     * @Return
     * @Exception
     * @Date 2022/3/18 15:34
     */
    @GetMapping("/toSecKillDetail/{id}")
    public String toSecKillDetail(@PathVariable String id,Model model){
        GoodsVo goods = goodsService.findGoodsVoById(id);
        Date startData = goods.getStartDate();
        Date endDate = goods.getEndDate();
        Date date = new Date();
        //设置状态  0为秒杀倒计时1为秒杀进行中2为秒杀结束
        int status ;
        //秒杀倒计时时间
        int remainSecond;
        if (date.before(startData)){
            status = 0;
            remainSecond = (int) ((startData.getTime() - date.getTime())/1000);
        }else if (date.after(endDate)){
            status = 2;
            remainSecond = -1;
        }else {
            status = 1;
            remainSecond = 0;
        }
        model.addAttribute("remainSecKill",remainSecond);
        model.addAttribute("status",status);
        //model.addAttribute("user",user);
        model.addAttribute("goods",goods);
        return "goodsDetails";
    }
    /*@GetMapping("/toDetail/{id}")
    public String toDetails(Model model,@PathVariable String id,HttpSession session,@CookieValue("userTicket") String ticket){
        if (StringUtils.isEmpty(ticket)){
            return "login";
        }
        User user = userService.getUserByTicket("user:"+ticket);
        if (user == null){
            return "login";
        }
        GoodsVo goods = goodsService.findGoodsVoById(id);
        Date startData = goods.getStartDate();
        Date endDate = goods.getEndDate();
        Date date = new Date();
        //设置状态  0为秒杀倒计时1为秒杀进行中2为秒杀结束
        int status ;
        //秒杀倒计时时间
        int remainSecond;
        if (date.before(startData)){
            status = 0;
            remainSecond = (int) ((startData.getTime() - date.getTime())/1000);
        }else if (date.after(endDate)){
            status = 2;
            remainSecond = -1;
        }else {
            status = 1;
            remainSecond = 0;
        }
        model.addAttribute("remainSecKill",remainSecond);
        model.addAttribute("status",status);
        model.addAttribute("user",user);
        model.addAttribute("goods",goods);
        return "goodsDetails";
    }*/

    @GetMapping("/testjvbj")
    public String test(){
    	return "test";
    }
    
    public void kkewb() {
    	
    }
    /**
     * @Method
     * @Author wzz
     * @Version  1.0
     * @Description 跳转到秒杀商品维护页面
     * @param
     * @Return
     * @Exception
     * @Date 2022/3/15 22:35
     */
    @GetMapping("/toSeckillGoodsWh")
    public String toSeckillGoodsWh(){
        return "seckillWh/seckillList";
    }
}
